var express = require('express');
var router = express.Router();
var mysql = require('mysql');//数据库路由
var connection=mysql.createConnection({//链接数据库
  host	:'localhost',
  port	:3306,
  user	:'root',
  password:'root',
  database:'music'//库名
});
/* GET home page. */
router.get('/', function(req, res, next) {
  res.send('手机后台管理');
});
 //刷新
  function initmusic(insertId,res){
     connection.query('SELECT * FROM `music-lists2` WHERE id=?',insertId,function(err,rows,fd){
        if(err){
          console.log(err);
        }else {
          console.log('所以歌曲请求成功');
          res.send(rows);
        }
  });
  }
//请求所有歌曲
router.get('/mephone', function(req, res, next) {
  connection.query('SELECT * FROM `music-lists2`',function(err,rows,fd){
  if(err){
    console.log(err);
  }else {
    console.log('所以歌曲请求成功');
    res.send(rows);
  }
  });
});
//增加音乐
router.post('/addphone', function(req, res, next) {
 
  let name  = req.body.name;
  let src  = req.body.src;
  let musicImgSrc  = req.body.musicImgSrc;
  connection.query('INSERT INTO `music-lists2`(`src`, `songname`, `musicImgSrc`) VALUES (?,?,?)',[src,name,musicImgSrc],function(err,rows,fd){
  if(err){
    console.log(err);
  }else {
    console.log('添加歌曲成功');
    res.send(rows);
    console.log(rows)
    var insertId = rows.insertId;
    console.log(insertId)
    // initmusic(insertId,res)
  }
  });
});
//删除音乐
router.post('/deletephone', function(req, res, next) {
   console.log(req.body)
   let Oid = req.body.songdata.id
   console.log(Oid)
    connection.query('SELECT * FROM `music-lists2` WHERE id=?',Oid,function(err,rows,fd){
  if(err){
    console.log(err);
  }else {
    console.log('所以歌曲请求成功');
    res.send(rows);
    var songId = rows[0].id
    deleteSong(songId)
  }
});
//删除函数
  function deleteSong(songId){
       connection.query('DELETE FROM `music-lists2` WHERE id=?',songId,function(err,rows,fd){
       if(err){
    console.log(err);
      }else {
        console.log('所以歌曲请求成功');
        // res.send(rows);
        
      }

    })
  }
 
});






module.exports = router;
